package com.tdd.zkconfig.utils;

import org.I0Itec.zkclient.ZkClient;

/**
 * @author tudedong
 * @description 操作数据库配置工具
 * @date 2020-06-14 14:54:37
 */
public class ChangeDataSourceConfigUtils {

    private static final String URL_PATH = "/zookeeper/sql-url";
    private static final String DRIVER_PATH = "/zookeeper/sql-driver";
    private static final String USERNAME_PATH = "/zookeeper/sql-username";
    private static final String PASSWORD_PATH = "/zookeeper/sql-password";


    public static void main(String[] args) {


        ZkClient zkClient = ZkClientUtils.getZkClient();

        createPath(zkClient,URL_PATH);
        //本地mysql数据源
        //zkClient.writeData(URL_PATH,"jdbc:mysql://localhost:3306/test");
        //远程mysql数据源
        zkClient.writeData(URL_PATH,"jdbc:mysql://47.101.41.95:3306/test");
        createPath(zkClient,DRIVER_PATH);
        zkClient.writeData(DRIVER_PATH,"com.mysql.jdbc.Driver");
        createPath(zkClient,USERNAME_PATH);
        zkClient.writeData(USERNAME_PATH,"");
        createPath(zkClient,PASSWORD_PATH);
        zkClient.writeData(PASSWORD_PATH,"");
    }


    private static void createPath(ZkClient zkClient,String path) {
        boolean exists = zkClient.exists(path);
        if(!exists){
            zkClient.createPersistent(path);
        }
    }
}
